package cn.lyvust.test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import java.awt.*;
import java.util.Locale;

public class ClassOne extends JFrame {
    private ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");
    private JTextField jisuan_text = new JTextField();
    private JTextField answer_text = new JTextField();

    public ClassOne() {
        this.setTitle("计算器");
        this.setBtn();
        this.setLabel();
        this.setBounds(100,100,500,550);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        //设置布局
        this.getContentPane().setLayout(null);
        this.setLayout(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ClassOne();
    }
    public void setLabel() {
        JLabel jisuan = new JLabel("计算式");
        jisuan.setFont(new Font("华文楷体", Font.CENTER_BASELINE, 25));
        jisuan.setBounds(20, 0, 80, 80);
        this.getContentPane().add(jisuan);
        //计算式的文本框
        jisuan_text.setBounds(100, 15, 300, 50);
        jisuan_text.setHorizontalAlignment(JTextField.LEFT);
        jisuan_text.setFont(new Font("华文楷体", Font.PLAIN, 20));
        this.getContentPane().add(jisuan_text);

        //结果
        JLabel answer = new JLabel("结    果");
        answer.setFont(new Font("华文楷体", Font.CENTER_BASELINE, 25));
        answer.setBounds(20, 55, 80, 80);
        this.getContentPane().add(answer);
        //结果的文本框
        answer_text.setBounds(100, 70, 300, 50);
        answer_text.setHorizontalAlignment(JTextField.LEFT);
        answer_text.setFont(new Font("华文楷体", Font.PLAIN, 20));
        this.getContentPane().add(answer_text);
    }
    public void setBtn() {
        // 按钮文本
        String[] arr =
                { "del",  "cls", "%",  "/",
                   "7",   "8",   "9",  "*",
                   "4",   "5",   "6",  "+",
                   "1",   "2",   "3",  "-",
                   "+/-", "0",   ".",  "=",
                };
        JButton[] btnArray = new JButton[arr.length];
        JPanel panel = new JPanel();
        panel.setBounds(20, 150, 400, 300);
        panel.setLayout(new GridLayout(5, 4, 8, 8));
        for (int i = 0; i < btnArray.length; i++) {
            btnArray[i] = new JButton(arr[i]);
            btnArray[i].setFont(new Font("黑体", Font.CENTER_BASELINE, 20));
            btnArray[i].setBackground(new Color(242, 240, 235));
            if (btnArray.length - 1 == i) { // 等于号单独设置一个颜色
                btnArray[i].setBackground(new Color(211, 120, 129));
            }
            btnArray[i].addActionListener(e -> {
                String btnValue = e.getActionCommand();
                if (btnValue.equals("del")) {
                    jisuan_text.setText("");
                } else if (btnValue.equals("cls")) {
                    jisuan_text.setText(jisuan_text.getText().substring(0, jisuan_text.getText().length()-1));
                } else if (btnValue.equals("+")) {
                    jisuan_text.setText(m1(jisuan_text, btnValue));
                } else if (btnValue.equals("-")) {
                    jisuan_text.setText(m1(jisuan_text, btnValue));
                } else if (btnValue.equals("*")) {
                    jisuan_text.setText(m1(jisuan_text, btnValue));
                } else if (btnValue.equals("/")) {
                    jisuan_text.setText(m1(jisuan_text, btnValue));
                } else if (btnValue.equals("%")) {
                    jisuan_text.setText(m1(jisuan_text, btnValue));
                } else if (btnValue.equals("=")) {
                    answer_text.setText(cal(jisuan_text.getText()));
                } else { // 0到9
                    jisuan_text.setText(jisuan_text.getText()+ btnValue);
                }
            });
            panel.add(btnArray[i]);
        }
        this.add(panel);
    }
    public String m1(JTextField jisuan_text, String string) {
        String str = jisuan_text.getText() + (jisuan_text.getText().endsWith(string) ? "" : string);
        return str;
    }
    public String cal(String text) {
        Object obj = null;
        try {
            obj = se.eval(text);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return obj.toString();
    }
}
